package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.PatientSurgeryList;
import com.woniuxy.entity.SfMoney;
import com.woniuxy.entity.SurgeryManage;
import com.woniuxy.entity.dto.PatientSurgeryDTO;
import com.woniuxy.mapper.DrugPatientMapper;
import com.woniuxy.mapper.PatientSurgeryListMapper;
import com.woniuxy.mapper.SfMoneyMapper;
import com.woniuxy.mapper.SurgeryManageMapper;
import com.woniuxy.service.PatientSurgeryListService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.SnowflakeIdGenerator;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PatientSurgeryListServiceImpl implements PatientSurgeryListService , SaasService {
    @Autowired
    PatientSurgeryListMapper listMapper;
    @Autowired
    private SurgeryManageMapper surgeryManageMapper;
    @Autowired
    SfMoneyMapper sfMoneyMapper;
    @Autowired
    DrugPatientMapper drugPatientMapper;

    @Override
    public ResponseData<?> queryList(PageParam<PatientSurgeryDTO> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<PatientSurgeryList> patientSurgeryLists = listMapper.queryList(pageParam.getParam());
        PageInfo pageInfo = new PageInfo(patientSurgeryLists);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<?> updateStatus(PatientSurgeryList patientSurgeryList) {
        BigDecimal sdSurgicalPrice = patientSurgeryList.getSdSurgicalPrice();//手术价格
        Integer id = patientSurgeryList.getId();//手术列表id
        Integer sdCaseId = patientSurgeryList.getSdCaseId();//患者id
        //先查病人钱包 再减去手术费用 重新将金额塞到钱包里
        PatientSurgeryList patientSurgeryList1 = drugPatientMapper.queryByPatientPurse(sdCaseId);
        BigDecimal purse = patientSurgeryList1.getPurse();//病人钱包
        BigDecimal balance = purse.subtract(sdSurgicalPrice);//余额
        PatientSurgeryList patientSurgeryList2 = new PatientSurgeryList();
        patientSurgeryList2.setPurse(balance);
        patientSurgeryList2.setSdCaseId(sdCaseId);
        sfMoneyMapper.update(patientSurgeryList2);
        //再修改状态
        listMapper.updateStatus("1",id);
        return new ResponseData<>().ok();
    }

    //添加手术申请
    @SneakyThrows
    @Override
    public ResponseData<?> addSMItem(PatientSurgeryDTO psDTO) {
        SurgeryManage surgeryManage = surgeryManageMapper.queryById(psDTO.getSdSiId());
        if (surgeryManage==null){
            return new ResponseData<>().fail(ResponseEnum.FAIL);
        }
        //手术申请对象
        PatientSurgeryList patientSurgeryList = new PatientSurgeryList();

        SnowflakeIdGenerator snow = new SnowflakeIdGenerator();
        long nextId = snow.nextId();
        //雪花id
        patientSurgeryList.setSdNo(String.valueOf(nextId));
        //手术项目价格
        patientSurgeryList.setSdSurgicalPrice(surgeryManage.getSiPrice());
        //手术项目id
        patientSurgeryList.setSdSiId(surgeryManage.getId());
        //手术项目名称
        patientSurgeryList.setSdSurgicalName(surgeryManage.getSiName());
        //病人id
        patientSurgeryList.setSdCaseId(psDTO.getSdCaseId());
        //主治医生id
        patientSurgeryList.setSdWorkerId(psDTO.getSdWorkerId());
        //预约手术时间
        patientSurgeryList.setSdDate(psDTO.getSdDate());
        //将数据 添加到t_sd中
        listMapper.addSMItem(patientSurgeryList);

        //添加手术缴费明细
        SfMoney sfMoney = new SfMoney();
        long sfNo = snow.nextId();
        sfMoney.setSfNo(String.valueOf(sfNo));
        sfMoney.setSfPrice(surgeryManage.getSiPrice());
        sfMoney.setSfCaseId(psDTO.getSdCaseId());
        sfMoney.setSfSurgicalid(String.valueOf(nextId));
        sfMoney.setSfSurgicalname(surgeryManage.getSiName());
        sfMoneyMapper.addSf(sfMoney);
        return new ResponseData<>().ok();
    }
}
